﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Common;
using CNCSoft.EData;

namespace CNCSoft.ErpApps.TMS.longGiaoVu
{
    public partial class frmDangKyGiaoVienLopAddition : Form
    {
        private SqlConnection conn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);
        private DataSet ds = new DataSet();
        private string _makhoahoc, _magiaovien;
        bool _insert = false;
        bool _edit = false;
        bool _dangday = false;
        double _rateusd, _ratevnd;
        public frmDangKyGiaoVienLopAddition()
        {
            InitializeComponent();
        }

        public frmDangKyGiaoVienLopAddition(string makhoahoc, string magiaovien, bool insert, bool edit, bool dangday, double rateusd, double ratevnd)
        {
            InitializeComponent();
            _magiaovien = magiaovien;
            _makhoahoc = makhoahoc;
            _insert = insert;
            _edit = edit;
            _dangday = dangday;
            _rateusd = rateusd;
            _ratevnd = ratevnd;
        }

        private void frmDangKyGiaoVienLopAddition_Load(object sender, EventArgs e)
        {
            LoadControl();
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            try
            {
                if (_insert)
                {
                    DangKy(_makhoahoc, _magiaovien, Convert.ToDouble((txt_USD.Text == "") ? "0" : txt_USD.Text),
                        Convert.ToDouble((txt_VND.Text == "") ? "0" : txt_VND.Text), memo_Note.Text);
                }
                else if (_edit)
                {
                    EditGiaoVien(_magiaovien, _makhoahoc, Convert.ToDouble(txt_USD.EditValue), Convert.ToDouble(txt_VND.EditValue), 
                                Convert.ToBoolean(checkEdit_DangDay.EditValue));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #region "Coding"
        private void LoadControl()
        {
            LoadData();
        }

        private void LoadData()
        {
            try
            {
                lbl_MaGiaoVien.Text = _magiaovien;
                lbl_MaLop.Text = _makhoahoc;
                if (_insert)
                {
                    lbl_TrangThai.Text = "Insert";
                    checkEdit_DangDay.Visible = false;
                    btn_OK.Text = "Insert";
                    txt_USD.Text = _rateusd.ToString();
                    txt_VND.Text = _ratevnd.ToString();
                    memo_Note.Visible = true;
                }
                else if (_edit)
                {
                    lbl_TrangThai.Text = "Edit";
                    checkEdit_DangDay.Visible = true;
                    checkEdit_DangDay.EditValue = _dangday;
                    btn_OK.Text = "Cập nhật";
                    txt_USD.Text = _rateusd.ToString();
                    txt_VND.Text = _ratevnd.ToString();
                    memo_Note.Visible = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DangKy(string makhoahoc, string magiaovien, double rateusd, double ratevnd, string note)
        {
            try
            {
                if (Validation())
                {
                    string sql = @"usp_InsertKhoaHocGiaoVien @makhoahoc = '" + makhoahoc + @"', @magiaovien = '" + magiaovien + @"',
                                    @rateusd = " + rateusd + @", @ratevnd = " + ratevnd + @", @note = '" + note + @"', @dangday = 1";
                    SQLHelper.ExecuteNonQuery(GlobalVariable.WSInfo.ConnectionString, sql);
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void EditGiaoVien(string magiaovien, string makhoahoc, double rateusd, double ratevnd, bool dangday)
        {
            string sql = String.Format("usp_UpdateKhoaHocGiaoVien @makhoahoc = {0}, @magiaovien = {1}, @rateusd = {2}, @ratevnd = {3}, @dangday = {4}",
                                        makhoahoc, magiaovien, rateusd, ratevnd, dangday);
            SQLHelper.ExecuteNonQuery(GlobalVariable.WSInfo.ConnectionString, sql);
            this.Close();
        }

        private bool Validation()
        {
            try
            {
                if (txt_USD.Text == "" && txt_VND.Text == "")
                {
                    MessageBox.Show("Ban chua nhap rate");
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
        }
        #endregion
    }
}
